package com.ga.wact.controller;

import com.ga.wact.common.PageBean;
import com.ga.wact.common.Result;
import com.ga.wact.common.ResultUtils;
import com.ga.wact.entity.*;
import com.ga.wact.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;

import static com.ga.wact.utils.StrToInt.StringToInteger;

/**
 * Created by 幻夜~星辰 on 2018/10/15.
 * 2018-10-21 判断是等于“”而不是null
 */
@Controller
@RequestMapping("/formOne")//
public class goAbroadController {


    @Autowired
    private FormOnePersonInformationService formOnePersonInformationService;


    //转到主页面
    @GetMapping(value = "/home")
    public String goHome() {
        return "home";
    }


    //转到添加页面
    @GetMapping(value = "/Add")
    public ModelAndView writeForm(Integer userId, @RequestParam(value = "currPage", defaultValue = "1", required = false) int currPage) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("formOneAdd");
        modelAndView.addObject("formOnePersonInformation", getInfo(userId, 0, currPage));

        return modelAndView;
    }


    //跳转到审核失败的界面
    @GetMapping(value = "/NotPass")
    public ModelAndView fail(Integer userId, @RequestParam(value = "currPage", defaultValue = "1", required = false) int currPage) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("formOneNotPass");
        modelAndView.addObject("formOnePersonInformations", getInfo(userId, 3, currPage));

        return modelAndView;
    }

    //跳转到审核通过的界面
    @GetMapping(value = "/Passed")
    public ModelAndView success(Integer userId, @RequestParam(value = "currPage", defaultValue = "1", required = false) int currPage) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("formOnePassed");
        modelAndView.addObject("formOnePersonInformations", getInfo(userId, 2, currPage));

        return modelAndView;
    }

    //跳转到查看审核中的信息的界面
    @GetMapping(value = "/Auditing")
    public ModelAndView Auditing(Integer userId, @RequestParam(value = "currPage", defaultValue = "1", required = false) int currPage) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("formOneAuditing");
        modelAndView.addObject("formOnePersonInformations", getInfo(userId, 1, currPage));
        return modelAndView;
    }


    //处理提交操作的controller
    //信息处理的controller,由于字段过多并且某些特定字段可以拥有多个值，暂时不采用json，采用HttpServletRequest接收
    @PostMapping(value = "/subInfo")
    @ResponseBody
    public Result<Object> add(FormOnePersonInformation formOnePersonInformation, HttpServletResponse response) {
        formOnePersonInformation.setStatus(1);
        formOnePersonInformationService.insert(formOnePersonInformation);
        return ResultUtils.success(null);
    }


    //处理保存操作的controller
    //?数据验证？？？valid？？？
    @PostMapping(value = "/saveInfo")
    @ResponseBody
    public Result<Object> save(@Valid FormOnePersonInformation formOnePersonInformation, BindingResult result, HttpServletResponse response, HttpServletRequest request) {
        formOnePersonInformation.setStatus(0);
        if (result.hasErrors()) {
            return ResultUtils.param_loss_error();
        } else {
            formOnePersonInformationService.insert(formOnePersonInformation);

            return ResultUtils.success(null);
        }

    }


    /**
     * 根据userId和状态获取表单信息
     * @param userId
     * @param status
     * @param currPage
     * @return
     */
    @GetMapping(value = "/getInfo")
    @ResponseBody
    public Result<Object> getInfo(Integer userId, Integer status, @RequestParam(value = "currPage", defaultValue = "1", required = false) int currPage) {
        // int userId=Integer.parseInt(request.getParameter("userId"));
        if (status == 0 || status == 1 || status == 2 || status == 3) {
            Map hashMap = new HashMap();
            hashMap.put("userId", userId);
            hashMap.put("status", status);
            hashMap.put("currPage", currPage);

            //0保存的信息，1已提交未审核，2审核通过，3审核不通
            PageBean<FormOnePersonInformation> formOnePersonInformations = formOnePersonInformationService.selectAll((HashMap) hashMap);

            //当审核失败时，得到原因.方便显示
            if (status == 3) {

            }
            return ResultUtils.success(formOnePersonInformations);
        } else {
            return ResultUtils.paramerror();
        }
    }


    /**
     * 获得单个信息
     *test码云
     * @param id
     * @return
     */
    @GetMapping(value = "/getOneInfo/{id}")
    @ResponseBody
    public Result<Object> getOneInfo(@PathVariable("id") Integer id) {
        return ResultUtils.success(formOnePersonInformationService.selectOneInfo(id));

    }


}
